<?php

class MemberAction extends BaseAction
{
	function __construct()
	{
		parent::__construct();
		$this->menuid = !empty($_GET['menuid']) ? $_GET['menuid'] : 19;
	}

	function Index()
	{
		$member_mod = D('member');
		import("ORG.Util.Page");
		$count = $member_mod->count();
		$p = new Page($count,5);

		$members_list = $member_mod->limit($p->firstRow.','.$p->listRows)->order('reg_time DESC')->findAll();

		$key = 1;
		foreach($members_list as $k=>$val){
			$members_list[$k]['key'] = ($p->nowPage-1) * ($p->listRows) + $key++;
		}
		$sub_menu = $this->admin_menu($this->menuid,true);
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=member&a=add\', title:\''.L('add_member').'\', width:\'400\', height:\'200\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('add_member'));
		$page = $p->show();
		$this->assign('page',$page);
		$this->assign('big_menu',$big_menu);
    	$this->assign('sub_menu',$sub_menu);
		$this->assign('members_list',$members_list);
		$this->display();
	}

	function add()
	{
		$this->display();
	}

	function insert()
	{
		$member_mod = D('member');

		if(!isset($_POST['nickname'])||($_POST['nickname']=='')){
			$this->error('用户昵称不能为空');
		}
		if($_POST['password'] != $_POST['repassword']){
			$this->error('两次输入的密码不相同');
		}
		$email = $_POST['email'];
		$result = $member_mod->where("email='".$email."'")->count();
		if($result){
		    $this->error('该邮箱已经注册过，不能再注册');
		}
		$vo = $member_mod->create();
		$vo['password'] = md5($vo['password']);
		$result = $member_mod->add($vo);
		if($result){
			$this->success(L('operation_success'), '', '', 'add');
		}else{
			$this->error(L('operation_failure'));
		}

	}

	function edit()
	{
		$member_mod = D('member');
		if( isset($_GET['uid']) ){
			$uid = isset($_GET['uid']) && intval($_GET['uid']) ? intval($_GET['uid']) : $this->error(L('please_select').L('member'));
		}
		$member_info = $member_mod->where('uid='.$uid)->find();
		$this->assign('member_info',$member_info);
		$this->display();
	}

	function update()
	{
		$member_mod = D('member');
		if($_POST['password'] != $_POST['repassword']){
			$this->error('两次输入的密码不相同');
		}
		$vo = $member_mod->create();
		$vo['password'] = md5($vo['password']);
		$result = $member_mod->save($vo);
		if(false !== $result){
			$this->success(L('operation_success'), '', '', 'edit');
		}else{
			$this->error(L('operation_failure'));
		}
	}


	function delete()
	{
		if((!isset($_GET['uid']) || empty($_GET['uid'])) && (!isset($_POST['uid']) || empty($_POST['uid']))) {
            $this->error('请选择要删除的会员！');
		}
		$member_mod = D('member');
		if (isset($_POST['uid']) && is_array($_POST['uid'])) {
		    $ids = implode(',', $_POST['uid']);
		    $member_mod->delete($ids);
		} else {
			$id = intval($_GET['uid']);
			$member_mod->delete($id);
		}
		$this->success(L('operation_success'));
	}

	function address_list()
	{
		$uid = isset($_GET['uid']) && intval($_GET['uid']) ? intval($_GET['uid']) : $this->error(L('please_select').L('member'));
		$address_mod = M('address');
		$member_mod = D('member');
		$add_list = $address_mod->where('uid='.$uid)->findAll();
		$email = $member_mod->field('email')->where('uid='.$uid)->find();

		foreach($add_list as $k=>$val){
			$add_list[$k]['email'] = $email['email'];
		}
		//dump($add_list);exit;
		$this->assign('add_list',$add_list);
		$this->display();
	}
}
?>